<?php
class menuAction extends backendAction {
    public function _initialize() {
        parent::_initialize();
        $this->_mod = D('menu');
    }

    public function index() {
        $tree = new Tree();
        $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ','&nbsp;&nbsp;&nbsp;├─ ','&nbsp;&nbsp;&nbsp;└─ ');
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
        $result = $this->_mod->order('ordid')->select();
        $array = array();
        foreach($result as $r) {
            $r['cname'] = $r['name'];
            $r['str_manage'] = '<a href="'.U(MODULE_NAME.'/add',array('menuid'=>$_GET['menuid'],'menupid'=>$_GET['menupid'],'pid'=>$r['id'])).'" class="btn blue">增加子菜单</a> 
							    <a href="'.U(MODULE_NAME.'/edit',array('menuid'=>$_GET['menuid'],'menupid'=>$_GET['menupid'],'id'=>$r['id'])).'" class="btn green">编辑</a>  
                                <a  class="btn red" onclick="del('.$r['id'].')">删除</a>';
            if($r['ico'])$r['ico']='<i class="'.$r['ico'].'"></i>';
            $array[] = $r;
        }
        $str  = "<tr class='odd gradeX'>
                <td align='center'><input type='checkbox' value='\$id' class='checkbox'></td>
                <td>\$spacer<span data-tdtype='edit' data-field='name' data-id='\$id'>\$name</span></td>
                <td align='center'>\$ico</td>
                <td align='center'><span data-tdtype='edit' data-field='ordid' data-id='\$id' class='tdedit'>\$ordid</span></td>
                <td align='center'>\$str_manage</td>
                </tr>";
        
		
		$tree->init($array);
        $menu_list = $tree->get_tree(0, $str);
        $this->assign('menu_list', $menu_list);

        $big_menu = array(
            'title' => '添加菜单',
            'iframe' => U('menu/add'),
            'id' => 'add',
            'width' => '500',
            'height' => '350',
        );
        $this->assign('big_menu', $big_menu);
        $this->assign('list_table', true);
        $this->display();
    }

    public function _before_add()
    {
        $tree = new Tree();
        $result = $this->_mod->select();
        $array = array();
        foreach($result as $r) {
            $r['selected'] = $r['id'] == $_GET['pid'] ? 'selected' : '';
            $array[] = $r;
        }
        $str  = "<option value='\$id' \$selected>\$spacer \$name</option>";
        $tree->init($array);
        $select_menus = $tree->get_tree(0, $str);
        $this->assign('select_menus', $select_menus);
    }

    public function _before_edit()
    {
        $id = $this->_get('id','intval');
        $info = $this->_mod->find($id);
        $this->assign('info', $info);
        $tree = new Tree();
        $result = $this->_mod->select();
        $array = array();
        foreach($result as $r) {
            $r['selected'] = $r['id'] == $info['pid'] ? 'selected' : '';
            $array[] = $r;
        }
        $str  = "<option value='\$id' \$selected>\$spacer \$name</option>";
        $tree->init($array);
        $select_menus = $tree->get_tree(0, $str);
        $this->assign('select_menus', $select_menus);
    }

}